
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author remichaoul
 */
public class Utilisateur {

    private InterfacePanier myPanier;
    private static Utilisateur instance = null;
    private InterfaceCatalogue myCatalogue;


    private Utilisateur(){
        myPanier = null;
    }

   public static Utilisateur getUtilisateur() {
      if(instance == null)
          instance = new Utilisateur();
      return instance;
   }

    public void choisirAction() throws RemoteException{
        ArrayList <Article> articleList = this.myCatalogue.findByAutor("HerrChaoul");
        
        for(Article art: articleList)
            System.out.println(art.getDescription());

        if(this.myPanier.AjouterArticle(articleList.get(0)))
            System.out.println("Ajout fait");

        for(Article art: this.myPanier.getArticle())
            System.out.println(art.getDescription());

        this.myPanier.RetirerArticle(this.myPanier.getArticle().get(0));

        if(this.myPanier.getArticle().isEmpty())
            System.out.println("ya plus rien");
    }

    public static void main(String[] args) throws RemoteException{
        Utilisateur u = Utilisateur.getUtilisateur();
        try{
            //System.out.println(Integer.valueOf(args[1]));
        	//if(System.getSecurityManager()==null)System.setSecurityManager(new SecurityManager());
            Registry registry = LocateRegistry.getRegistry(args[1],Integer.valueOf(args[0]));
        	//Registry registry = LocateRegistry.getRegistry("localhost",1099);
            InterfacePanier panier = (InterfacePanier) registry.lookup("panier");
            u.myPanier=panier;
            
            
        }

        catch(Exception e){
                e.printStackTrace();
        }


        try{
            //if(System.getSecurityManager()==null)System.setSecurityManager(new SecurityManager());
            Registry registry = LocateRegistry.getRegistry("localhost",1099);
            InterfaceCatalogue catalogue = (InterfaceCatalogue) registry.lookup("catalogue");
            u.myCatalogue=catalogue;
        }
        catch(Exception e){
            e.printStackTrace();
        }


        u.choisirAction();


    }

}
